package com.ynet.middleground.mobile.approve.controller;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.dozer.Mapper;
import org.slf4j.helpers.MessageFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ynet.middleground.mobile.approve.commom.ResponseResult;
import com.ynet.middleground.mobile.approve.constant.MsgTypeEnum;
import com.ynet.middleground.mobile.approve.dao.ReceiveOperateMqLogMapper;
import com.ynet.middleground.mobile.approve.entity.ReceiveOperateMqLog;
import com.ynet.middleground.mobile.approve.mq.producer.SourceMobileApproveProducer;
import com.ynet.middleground.mobile.approve.mq.producer.SourceProducer;
import com.ynet.middleground.mobile.approve.response.UserDTO;
import com.ynet.middleground.mobile.approve.service.MessageService;
import com.ynet.middleground.mobile.approve.service.UserCenterService;
import com.ynet.middleground.mobile.approve.utils.ApiHelper;

import springfox.documentation.annotations.ApiIgnore;

/**
 * @author liulx
 * @description 仅供测试
 * @date 2020-11-24 15:15
 */
@RestController
@RequestMapping("test")
@ApiIgnore
public class TestController {

    @Autowired
    UserCenterService userCenterService;

    @Autowired
    private Mapper mapper;

    @Autowired
    private SourceProducer sourceProducer;

    @Autowired
    private SourceMobileApproveProducer sourceMobileApproveProducer;

    @Autowired
    private ReceiveOperateMqLogMapper receiveOperateMqLogMapper;

    /**
     * 普通数据示例
     *
     * @return
     */
    @RequestMapping("/hello")
    public String hello() {
        return "helloooooo";
    }

    /**
     * 用户中台调用示例-查询用户
     *
     * @return
     */
    @RequestMapping("/query")
    public Object invokeUser() {
        return userCenterService.getUserInfo(361, "T01", 361, 361);
    }

    /**
     * 用户中台调用示例-查询渠道列表
     *
     * @return
     */
    @RequestMapping("/channel/list")
    public Object invokeUserChannel() {
        return userCenterService.channelList();
    }

    public static void main(String[] args) {
        try {
            SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
            long between = 0;
            // Date begin = dfs.parse("2020-10-13 05:55:48.009");
            Date begin = dfs.parse("2020-10-13 05:55:49.587");
            Date end = dfs.parse("2020-10-13 05:56:08.617");
            // 得到两者的毫秒数
            between = (end.getTime() - begin.getTime());
            System.out.println("between = " + between);
            String[] contents = {"呵呵", "花儿"};
            String template = MessageFormatter.arrayFormat("哈哈哈哈 {} name: {}", contents).getMessage();
            System.out.println("template = " + template);
        } catch (Exception e) {

        }
    }

    @Autowired
    AuthController authController;

    @Autowired
    ApiHelper apiHelper;

    @PostMapping("getUserAvatarByUserId")
    public ResponseResult<UserDTO> getUserInfoById(@RequestParam String userId) {
        String accessToken = apiHelper.getAccessToken();
        return authController.getUser(accessToken, userId);
    }

    @RequestMapping("/mq")
    public @ResponseBody String mqTest(String abc) {
        sourceProducer.sendMessages(abc);
        return "success";
    }

    @RequestMapping("/mqInsert")
    public String testInsert() {
        Map<String, Object> msgMap = new HashMap<>(16);
        msgMap.put("traceId", "12341234");
        msgMap.put("requestTime", LocalDateTime.now());
        msgMap.put("channel", "T01");
        msgMap.put("hasCommand", false);
        msgMap.put("commandName", "12341234");
        msgMap.put("reason", "12341234");
        msgMap.put("taskId", "530055");
        msgMap.put("allowApproveOperate", true);
        msgMap.put("commandResult", true);
        ReceiveOperateMqLog receiveOperateMqLog = mapper.map(msgMap, ReceiveOperateMqLog.class);
        receiveOperateMqLog.setGmtCreate(LocalDateTime.now());
        receiveOperateMqLogMapper.insert(receiveOperateMqLog);
        return "success";
    }

    @RequestMapping("/mapper")
    public void testMapper() {
        Map<String, Object> msgMap = new HashMap<>(16);
        msgMap.put("traceId", "12341234");
        msgMap.put("requestTime", LocalDateTime.now());
        msgMap.put("channel", "T01");
        msgMap.put("hasCommand", false);
        msgMap.put("commandName", "12341234");
        msgMap.put("reason", "12341234");
        msgMap.put("taskId", "530055");
        msgMap.put("allowApproveOperate", true);
        msgMap.put("commandResult", true);

        String requestTime = String.valueOf(msgMap.get("requestTime"));
        msgMap.remove("requestTime");
        ReceiveOperateMqLog operateMqLog = mapper.map(msgMap, ReceiveOperateMqLog.class);
        operateMqLog.setGmtCreate(LocalDateTime.now());
        operateMqLog.setRequestTime(LocalDateTime.parse(requestTime));
        System.out.println("operateMqLog = " + operateMqLog);
    }

    @Autowired
    MessageService messageService;

    @RequestMapping("/push-ding-msg")
    public void testGetNextApproverAndPushMsg() {
        messageService.sendDingtalkMessage(MsgTypeEnum.TODO, 4015, "1850001", null);
    }
}
